package com.yycx.module.file.provider.service.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.constants.FileTypeConstant;
import com.yycx.common.enums.StateEnum;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.module.file.client.entity.SysFileType;
import com.yycx.module.file.provider.mapper.SysFileTypeMapper;
import com.yycx.module.file.provider.service.SysFileTypeService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件类型接口实现类
 *
 * @author flyme
 * @date 2021-12-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class SysFileTypeServiceImpl extends BaseServiceImpl<SysFileTypeMapper, SysFileType> implements SysFileTypeService {


    @PostConstruct
    public void init() {
        log.info("*******************************初始化上传文件类型****************************");
        this.initFileType();
    }

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, SysFileType sft, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<SysFileType> cq, SysFileType sft, EntityMap requestMap) {
        cq.orderByDesc("sft.createTime");
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> selectAccept(String group) {
        CriteriaQuery cq = new CriteriaQuery(SysFileType.class);
        cq.select("typeName", "allowMaxSize");
        if (FlymeUtils.isNotEmpty(group)) {
            String[] groupArray = group.split(",");
            cq.in("fileGroup", groupArray);
        }
        cq.eq("state", CommonConstants.ENABLED);
        List<EntityMap> fileTypeList = selectEntityMap(cq);
        return fileTypeList;
    }

    @Override
    public List<EntityMap> listByUsed() {
        CriteriaQuery cq = new CriteriaQuery(SysFileType.class);
        cq.eq(true, "useState", CommonConstants.ENABLED);
        List<EntityMap> fileTypeList = selectEntityMap(cq);
        return fileTypeList;
    }

    /**
     * 初始化上传文件配置
     *
     * @return
     */
    public  Map<Object, Object>  initFileType() {
        CriteriaQuery cq = new CriteriaQuery(SysFileType.class);
        cq.select("*");
        cq.eq("state", CommonConstants.ENABLED);
        List<EntityMap> list = selectEntityMap(cq);
        if (FlymeUtils.isNotEmpty(list)) {
            Map<Object, Object> fileTypeMap =  redisUtils.getMapByProject(FileTypeConstant.RedisKey);
            if (FlymeUtils.isEmpty(fileTypeMap)) {
                fileTypeMap = new HashMap<>();
                for (EntityMap entityMap : list) {
                    String typeName = entityMap.get("typeName");
                    fileTypeMap.put(typeName, entityMap);
                }
                redisUtils.setProjectMap(FileTypeConstant.RedisKey, fileTypeMap);
            }
            return fileTypeMap;
        }
        return null;
    }

    @Override
    public ResultBody setStatus(Long fileTypeId) {
        ResultBody resultBody = new ResultBody();
        SysFileType fileType = getById(fileTypeId);
        Integer stateEnum = fileType.getState();
        CriteriaUpdate cu = new CriteriaUpdate();
        cu.eq("fileTypeId", fileTypeId);
        if (stateEnum.equals(StateEnum.ENABLE.getCode())) {
            cu.set(true, "state", StateEnum.DISABLE.getCode());
            resultBody.setMsg(StateEnum.DISABLE.getName() + "成功").data(0);
        } else {
            cu.set(true, "state", StateEnum.ENABLE);
            resultBody.setMsg(StateEnum.ENABLE.getName() + "成功").data(1);
        }
        update(cu);
        initFileType();
        return resultBody;

    }
}
